﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;


namespace DN_CtrDisplay
{
    class CtrDisplay
    {
        private delegate void EnableCtrEventHandler(Control ctr, bool enable);

        private delegate void ShowTextControlEventHandler(Control control, string s);

        private delegate void ShowTextControlWithColorEventHandler(Control control, string s, Color color);

        private delegate void ShowTextFormEventHandler(Form form, string s);

        private delegate void ShowTextFromWithColorEventHandler(Form form, string s, Color color);

        private delegate void ShowTextStatusEventHandler(ToolStripStatusLabel statusControl, string s);

        private delegate void ShowTextStatusWithColorEventHandler(ToolStripStatusLabel statusControl, string s, Color color);

        private delegate void ShowTextOnDgvWithColorEventHandler(DataGridView dgv, int row, int cell,string s, Color c);

        private delegate void ShowTextOnDgvEventHandler(DataGridView dgv, int row, int cell, string s);

        private delegate void AddRowOnDgvEventHandler(DataGridView dgv, object[] objs);

        private delegate void AddRowOnDgvColorEventHandler(DataGridView dgv, object[] objs,Color c);

        private delegate void ChangeCellColorOnEventHandler(DataGridView dgv, int row, int cell, Color c);


        private delegate void ChangeCellAndColorEventHandler(DataGridView dgv, int row, int cell, string content, Color c);

        private delegate void ChangeCellContentEventHandler(DataGridView dgv, int row, int cell, string content);


        private delegate void ChangeRowEventHandler(DataGridView dgv, int row, object[] objs);

        private delegate void ChangeRowWithColorEventHandler(DataGridView dgv, int row, object[] objs, Color c);

        private delegate void ClearRichTextBoxEventHandler(RichTextBox rtBox);

        private delegate void AddTextToRichTextBoxEventHandler(RichTextBox rtBox, string s, bool addTime);

        private delegate void AddTextWithColorToRichTextBoxEventHandler(RichTextBox rtBox, string s, Color color, bool addTime);

        private delegate void AddTextWithFontToRichTextBoxEventHandler(RichTextBox rtBox, string s, Font font, bool addTime);

        private delegate void AddTextWithColorFontToRichTextBoxEventHandler(RichTextBox rtBox, string s, Color color, Font font, bool addTime);

        private delegate void GetLinesFromRichTextBoxEventHandler(RichTextBox rtBox,out string[] stringLines);

//        private delegate void AddHimageToPictureBoxEventHandler(PictureBox  pbx,HImage image);

        public static void button_MouseEnter(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.Font = new Font("微软雅黑", 12, FontStyle.Bold);
            //btn.BackColor = SystemColors.Control;
            btn.ForeColor = Color.Green;
            btn.FlatAppearance.BorderSize = 1;

        }
       
        public static void button_MouseLeave(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.Font = new Font("微软雅黑", 12);
            btn.ForeColor = SystemColors.ControlText;
            btn.FlatAppearance.BorderSize = 0;
        }

        public static void EnableCtr(Control ctr, bool enbale)
        {
            if (ctr.InvokeRequired)
            {
                ctr.Invoke(new EnableCtrEventHandler(EnableCtr), new object[] { ctr, enbale });
                return;
            }
            ctr.Enabled = enbale;
        }


        public static void ShowText(Control control, string s)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new ShowTextControlEventHandler(ShowText), new object[] { control, s });
                return;
            }
            control.Text = s;

        }

        public static void ShowText(Form form, string s)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new ShowTextFormEventHandler(ShowText), new object[] { form, s });
                return;
            }
            form.Text = s;

        }

        public static void ShowText(ToolStripStatusLabel statusControl, string s)
        {
            if (statusControl.Owner.InvokeRequired)
            {
                statusControl.Owner.Invoke(new ShowTextStatusEventHandler(ShowText), new object[] { statusControl, s });
                return;
            }
            statusControl.Text = s;

        }

        public static void ShowText(ToolStripStatusLabel statusControl, string s, Color color)
        {
            if (statusControl.Owner.InvokeRequired)
            {
                statusControl.Owner.Invoke(new ShowTextStatusWithColorEventHandler(ShowText), new object[] { statusControl, s, color });
                return;
            }
            statusControl.Text = s;
            statusControl.BackColor = color;
        }


        public static void ShowText(Form form, string s,Color color)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new ShowTextFromWithColorEventHandler(ShowText), new object[] { form, s ,color});
                return;
            }
            form.Text = s;
            form.BackColor = color;
        }


        public static void ShowText(Control control, string s, Color color)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new ShowTextControlWithColorEventHandler(ShowText), new object[] { control, s, color });
                return;
            }
            control.Text = s;
            control.BackColor = color;
        }

        public static void ShowTextForeColor(DataGridView dgv, int row, int cell, string s, Color c)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new ShowTextOnDgvWithColorEventHandler(ShowTextForeColor), new object[] { dgv,row,cell, s, c });
                return;
            }
            dgv.Rows[row].Cells[cell].Value = s;
            dgv.Rows[row].Cells[cell].Style.ForeColor = c;
        }


        public static void ShowTextBackColor(DataGridView dgv, int row, int cell, string s, Color c)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new ShowTextOnDgvWithColorEventHandler(ShowTextForeColor), new object[] { dgv, row, cell, s, c });
                return;
            }
            dgv.Rows[row].Cells[cell].Value = s;
            dgv.Rows[row].Cells[cell].Style.BackColor = c;
        }

        public static void ShowText(DataGridView dgv, int row, int cell, string s)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new ShowTextOnDgvEventHandler(ShowText), new object[] { dgv, row, cell, s, });
                return;
            }
            dgv.Rows[row].Cells[cell].Value = s;
        }

        public static void AddRowOnDgv(DataGridView dgv,object[] objs)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new AddRowOnDgvEventHandler(AddRowOnDgv), new object[] { dgv, objs });
                return;
            }
            dgv.Rows.Add();
            for (int i = 0; i < objs.Length; i++)
            {
                if (i < objs.Length)
                {
                    dgv.Rows[dgv.RowCount - 1].Cells[i].Value = objs[i].ToString();
                }
            }

            dgv.FirstDisplayedScrollingRowIndex = dgv.RowCount - 1;
        }

        public static void AddRowOnDgvColor(DataGridView dgv, object[] objs,Color c)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new AddRowOnDgvColorEventHandler(AddRowOnDgvColor), new object[] { dgv, objs, c });
                return;
            }
            dgv.Rows.Add();
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                if (i < objs.Length)
                {
                    dgv.Rows[dgv.RowCount - 1].Cells[i].Value = objs[i].ToString();
                    dgv.Rows[dgv.RowCount - 1].Cells[i].Style.ForeColor = c;
                }
            }
            dgv.FirstDisplayedScrollingRowIndex = dgv.RowCount - 1;
        }

        public static void ChangeCellColor(DataGridView dgv, int row, int cell, Color c)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new ChangeCellColorOnEventHandler(ChangeCellColor), new object[] { dgv, row, cell , c });
                return;
            }
            dgv.Rows[row].Cells[cell].Style.ForeColor = c;
        }


        public static void ChangeCellContent(DataGridView dgv, int row, int cell, string content)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new ChangeCellContentEventHandler(ChangeCellContent), new object[] { dgv, row, cell, content });
                return;
            }
            dgv.Rows[row].Cells[cell].Value = content;

        }

        public static void ChangeCellAndColor(DataGridView dgv, int row, int cell, string content, Color c)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new ChangeCellAndColorEventHandler(ChangeCellAndColor), new object[] { dgv, row, cell, content,c });
                return;
            }
            dgv.Rows[row].Cells[cell].Value = content;
            dgv.Rows[row].Cells[cell].Style.BackColor = c;

        }

        public static void ChangeRow(DataGridView dgv, int row, object[] objs)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new ChangeRowEventHandler(ChangeRow), new object[] { dgv, row,objs });
                return;
            }
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                if (i < objs.Length)
                {
                    dgv.Rows[row].Cells[i].Value = objs[i].ToString();
                }
            }
        }

        public static void ChangeRowColor(DataGridView dgv, int row, object[] objs, Color c)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new ChangeRowWithColorEventHandler(ChangeRowColor), new object[] { dgv, row, objs, c });
                return;
            }
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                if (i < objs.Length)
                {
                    dgv.Rows[row].Cells[i].Value = objs[i].ToString();
                }
            }
            dgv.Rows[row].DefaultCellStyle.BackColor = c;
        }

        public static void AddTextToRichTextBox(RichTextBox rtbox, string s, bool addTime)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new AddTextToRichTextBoxEventHandler(AddTextToRichTextBox), new object[] { rtbox, s, addTime });
                return;
            }

            if (addTime)
            {
                s = string.Format("【{0}】--{1}", DateTime.Now.ToString(), s);
            }

            rtbox.AppendText(s + "\n");

            rtbox.ScrollToCaret();
        }

        public static void AddTextToRichTextBox(RichTextBox rtbox, string s, Color color,bool addTime)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new AddTextWithColorToRichTextBoxEventHandler(AddTextToRichTextBox), new object[] { rtbox, s, color, addTime });
                return;
            }

            if (addTime)
            {
                s = string.Format("【{0}】--{1}", DateTime.Now.ToString(), s);
            }

            rtbox.SelectionColor = color;

            rtbox.AppendText(s + "\n");

            rtbox.ScrollToCaret();
        }

        public static void AddTextToRichTextBox(RichTextBox rtbox, string s, Font font,bool addTime)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new AddTextWithFontToRichTextBoxEventHandler(AddTextToRichTextBox), new object[] { rtbox, s, font,addTime });
                return;
            }

            if (addTime)
            {
                s = string.Format("【{0}】--{1}", DateTime.Now.ToString(), s);
            }

            rtbox.SelectionFont = font;

            rtbox.AppendText(s + "\n");

            rtbox.ScrollToCaret();
        }

        public static void AddTextToRichTextBox(RichTextBox rtbox, string s, Color color, Font font,bool addTime)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new AddTextWithColorFontToRichTextBoxEventHandler(AddTextToRichTextBox), new object[] { rtbox, s, color, font ,addTime});
                return;
            }

            if (addTime)
            {
                s = string.Format("【{0}】--{1}", DateTime.Now.ToString(), s);
            }

            rtbox.SelectionColor = color;

            rtbox.SelectionFont = font;

            rtbox.AppendText(s + "\n");

            rtbox.ScrollToCaret();
        }

        public static void ClearRichTextBox(RichTextBox rtbox)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new ClearRichTextBoxEventHandler(ClearRichTextBox), new object[] { rtbox });
                return;
            }

            rtbox.ResetText();
        }

        public static void GetAllText(RichTextBox rtbox ,out string[] stringLines)
        {
            if (rtbox.InvokeRequired)
            { 
                stringLines = null;
                rtbox.Invoke(new GetLinesFromRichTextBoxEventHandler(GetAllText), new object[] { rtbox });
                return;
            }

            stringLines = rtbox.Lines;
        }

        public static void dataGridView_Resize(object sender, int FirstColumnsWidth)
        {
            DataGridView dgv = (DataGridView)sender;
            foreach (DataGridViewColumn col in dgv.Columns)
            {
                col.Width = (dgv.Width) / dgv.ColumnCount;
                if (col.Width < 50) col.Width = 70;
            }
            if (FirstColumnsWidth > 0)
            {
                dgv.Columns[0].Width = FirstColumnsWidth;
            }
            foreach (DataGridViewRow row in dgv.Rows)
            {
                row.Height = (dgv.Height) / 9;

            }

        }




        //public static void AddHimageToPictureBox(PictureBox pbx, HImage  image)
        //{
        //    if (pbx.InvokeRequired)
        //    {
        //        pbx.Invoke(new AddHimageToPictureBoxEventHandler(AddHimageToPictureBox), new object[] { pbx, image });
        //        return;
        //    }
        //}

    }
}
